<?php

namespace App\Http\Controllers;

use  Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class BuildingController extends Controller
{
    public function index()
    {
        return view('building/index');
    }

    public function getList(Request $request)
    {
        $params = $request->all();

        if (empty($params['community_id'])) {
            response_json(1, '参数丢失，请联系管理员！');
        }

        $params['page_size'] = empty($params['page_size']) ? 15 : $params['page_size'];
        $params['current_page'] = empty($params['current_page']) ? 1 : $params['current_page'];

        $where = [['is_del', '=', '0']];
        $where[] = ['community_id', '=', $params['community_id']];

        $data = DB::table('building')
            ->select('id', 'name', 'community_id', 'description')
            ->where($where)
            ->paginate($params['page_size'], ['*'], 'page', $params['current_page']);

        response_json(0, '', $data);
    }

    public function getOne(Request $request)
    {
        $params = $request->all();
        if (empty($params['id'])) {
            response_json(1, '参数丢失，请联系管理员！');
        }

        $building = Db::table('building')->where('id', $params['id'])->first();
        $building->pics = getFile($building->pics);

        // 根据楼栋id和楼层数，查询楼层数据
        $floor = [];
        for ($i = 1; $i <= $building->floor_num; $i ++) {
            $room = Db::table('room')
                ->where('build_id', $params['id'])
                ->where('floor_num', $i)
                ->get();
            // 数据处理
            foreach ($room as $key => $value) {
                $room[$key]->status = $value->status . '';
                $room[$key]->pics = getFile($value->pics);
            }
            $floor[$i] = $room;
        }

        // 组装数据
        $data = [
            'building' => $building,
            'floor' => $floor,
        ];

        response_json($building ? 0 : 1, $building ? '' : '参数有误，请联系管理员！', $data);
    }

    public function save(Request $request)
    {
        $params = $request->all();
        // 数据校验
        $data['build_num'] = empty($params['build_num']) ? '' : $params['build_num'];
        $data['name'] = empty($params['name']) ? '' : $params['name'];
        $data['description'] = empty($params['description']) ? '' : $params['description'];
        $data['remarks'] = empty($params['remarks']) ? '' : $params['remarks'];
        $data['number'] = empty($params['number']) ? 0 : $params['number'];
        $data['floor_num'] = empty($params['floor_num']) ? 0 : $params['floor_num'];
        $data['community_id'] = empty($params['community_id']) ? 0 : $params['community_id'];
        $data['pics'] = empty($params['pics']) ? '' : getPics($params['pics']);

        if (isset($params['id'])) {
            $id = $params['id'];
            $ret = Db::table('building')->where('id', $id)->update($data);
        } else {
            $data['add_time'] = time();
            $id = $ret = Db::table('building')->insertGetId($data);
        }

        response_json($ret ? 0 : 1, $ret ? '保存成功！' : '保存失败，请联系管理员！', ['id' => $id]);
    }

    public function detail()
    {
        return view('building/detail');
    }

    public function roomSave(Request $request)
    {
        $params = $request->all();
        // 数据校验
        $data['floor_num'] = empty($params['floor_num']) ? 0 : $params['floor_num'];
        $data['room_num'] = empty($params['room_num']) ? '' : $params['room_num'];
        $data['name'] = empty($params['name']) ? '' : $params['name'];
        $data['area'] = empty($params['area']) ? 0 : $params['area'];
        $data['contact_name'] = empty($params['contact_name']) ? '' : $params['contact_name'];
        $data['phone'] = empty($params['phone']) ? '' : $params['phone'];
        $data['status'] = empty($params['status']) ? 0 : $params['status'];
        $data['description'] = empty($params['description']) ? '' : $params['description'];
        $data['remarks'] = empty($params['remarks']) ? '' : $params['remarks'];
        $data['build_id'] = empty($params['build_id']) ? 0 : $params['build_id'];
        $data['pics'] = empty($params['pics']) ? '' : getPics($params['pics']);

        if (isset($params['id'])) {
            $id = $params['id'];
            $ret = Db::table('room')->where('id', $id)->update($data);
        } else {
            $data['add_time'] = time();
            $id = $ret = Db::table('room')->insertGetId($data);
        }

        response_json($ret ? 0 : 1, $ret ? '保存成功！' : '保存失败，请联系管理员！', ['id' => $id]);
    }
}
